﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SetSailUpgrade
{
    public class WindowsHelper
    {
        /// <summary>
        /// 查询当前系统是否已经安装过软件
        /// </summary>
        /// <param name="appKeyName"></param>
        /// <returns></returns>
        public static bool CheckIfAppInstall(string appKeyName)
        {
            bool result = false;
            try
            {
                Microsoft.Win32.RegistryKey uninstallNode = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
                foreach (string subKeyName in uninstallNode.GetSubKeyNames())
                {
                    Microsoft.Win32.RegistryKey subKey = uninstallNode.OpenSubKey(subKeyName);
                    object displayName = subKey.GetValue("DisplayName");
                    if (displayName != null)
                    {
                        if (displayName.ToString().Contains(appKeyName))
                        {
                            result = true;
                            break;
                        }

                    }
                }
            }
            catch (Exception e)
            {
                string error = $"error in CheckIfAppInstall：{e.Message}";
                //MessShow.ShowMistake(error);
            }
            return result;
        }

        /// <summary>
        /// 获取EXE版本信息
        /// </summary>
        /// <param name="path">exe程序地址</param>
        /// <returns></returns>
        public static string GetVersion(string path)
        {
            string version = string.Empty;
            FileVersionInfo file = System.Diagnostics.FileVersionInfo.GetVersionInfo(path);
            //版本号显示为“主版本号.次版本号.内部版本号.专用部件号”。
            //version = String.Format("{0}.{1}.{2}.{3}", file.FileMajorPart, file.FileMinorPart, file.FileBuildPart, file.FilePrivatePart);
            //使用文件版本信息
            return file.FileVersion;
        }
    }
}
